﻿using BCCommon;
using BCEntity.TMS.User.UserInvoiceThin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using BCCommon.TMS.Invoice;
using System.Data;

namespace BCData.TMS.User.UserInvoiceThin
{
    public class UserInvoiceThinData : IUserInvoiceThinData
    {
        private readonly IDatabaseContext database;

        private const string QUERY = @"SELECT
		                            UserInvoiceThinId,
		                            UserId,
		                            InvoiceTitleType,
		                            CompanyName,
		                            BusinessLicenseNo,
		                            CompanyFinanceNo,
		                            CompanyPhone,
		                            CompanyAddress,
		                            BankName,
		                            CreateTime,
		                            UpdateTime 
                                FROM
                                    UserInvoiceThin";

        public UserInvoiceThinData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<UserInvoiceThinEntity> Insert(UserInvoiceThinEntity entity)
        {
            return await database.Master.InsertAsync(entity);
        }

        public async Task<UserInvoiceThinEntity> GetEntity(long userInvoiceThinId)
        {
            return await database.Slave.QueryEntityAsync<UserInvoiceThinEntity>($"{QUERY} WHERE UserInvoiceThinId = ?UserInvoiceThinId", new { userInvoiceThinId });
        }

        public async Task<UserInvoiceThinEntity> Update(UserInvoiceThinEntity entity)
        {
            return await database.Master.UpdateAsync(entity);
        }

        public async Task<int> Delete(long userInvoiceThinId)
        {
            return await database.Master.ExecuteAsync("DELETE FROM UserInvoiceThin WHERE UserInvoiceThinId = ?UserInvoiceThinId", new { userInvoiceThinId });
        }

        public async Task<(IEnumerable<UserInvoiceThinEntity>, int)> Query(long userId, InvoiceTitleType? invoiceTitleType, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("UserId = ?UserId");
            parameters.Add("?UserId", userId, DbType.Int64);
            if (invoiceTitleType.HasValue)
            {
                wheres.Add("InvoiceTitleType = ?InvoiceTitleType");
                parameters.Add("?InvoiceTitleType", invoiceTitleType.Value.ToString(), DbType.AnsiString);
            }
            string sql = string.Format(@"{0} {1} ORDER BY UserInvoiceThinId DESC LIMIT {2},{3};
                            SELECT COUNT(1) FROM UserInvoiceThin {1}", 
                            QUERY,
                            wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty,
                            (pageIndex - 1) * pageSize,
                            pageSize);
            return await database.Slave.QueryPageableListAsync<UserInvoiceThinEntity>(sql, parameters);
        }

        public async Task<IEnumerable<UserInvoiceThinEntity>> GetList(long userId, InvoiceTitleType? invoiceTitleType)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("UserId = ?UserId");
            parameters.Add("?UserId", userId, DbType.Int64);
            if (invoiceTitleType.HasValue)
            {
                wheres.Add("InvoiceTitleType = ?InvoiceTitleType");
                parameters.Add("?InvoiceTitleType", invoiceTitleType.Value.ToString(), DbType.AnsiString);
            }
            string sql = string.Format(@"{0} {1} ORDER BY UserInvoiceThinId DESC",QUERY, wheres.Count > 0? " WHERE " + string.Join(" AND ",wheres):string.Empty);
            return await database.Slave.QueryAsync<UserInvoiceThinEntity>(sql, parameters);
        }
    }
}
